/**
 * 混合路由,在身份验证通过(或者没有进行身份验证)的前提下,公用的路由
 * @type {createApplication}
 */
const express = require('express')
const bodyParser = require('body-parser')

//获取中间件和工具类
const userMid = require('../middleware/userMid')
const goodsMid = require('../middleware/goodsMid')
const goodsCategoryMid = require('../middleware/goodsCategoryMid')
const rootCategoryMid = require('../middleware/rootCategoryMid')
const orderMid = require('../middleware/orderMid')
const orderListMid = require('../middleware/orderListMid')
const messageMid = require('../middleware/messageMid')

const mixApp = express() // 混合路由子应用

/**配置post*/
mixApp.use(bodyParser.urlencoded({ extended: false }))//解析post请求数据
mixApp.use(bodyParser.json());//数据JSON类型
/**配置post*/

/***
 * 通过用户名查询用户
 */
mixApp.get('/search-user', [userMid.gUserByUsername], (request, response, next) => {
  if (request.user) { // 如果用户存在
    let res = {
      'code': 200,
      'mes': '用户存在',
      'user': request.user
    }
    response.status(200).send(res)
  } else {
    let res = {
      'code': 200,
      'mes': '用户不存在'
    }
    response.status(200).send(res)
  }
})


/**
 * 添加商品
 */
mixApp.post('/add-goods', [goodsMid.sGoods], (request, response, next) => {
  if (request.add_gid) {
    let res = {
      'code': 200,
      'msg': '添加商品成功'
    }
    response.status(200).send(res);
  } else {
    let res = {
      'code': 500,
      'msg': '添加商品失败！'
    }
    response.status(500).send(res)
  }
})

/**
 * 删除商品
 */
mixApp.post('/delete-goods', goodsMid.dGoods, (request, response, next) => {
  if (request.affectR) {
    let res = {
      'code': 200,
      'msg': '删除商品成功'
    };
    response.status(200).send(res);
  } else {
    let res = {
      'code': 500,
      'msg': '删除商品失败'
    };
    response.status(500).send(res);
  }
})

/**
 * 修改商品信息
 */
mixApp.post('/update-goods', [goodsMid.uGoods], (request, response, next) => {
  if(request.affectR > 0) {   //如果受影响行数存在，即修改成功
    let data = {
      'code': 200,
      'msg': '修改商品信息成功'
    };
    response.status(200).send(data);
  } else { // 否则,即未修改成功
    let data = {
      'code': 500,
      'msg': '修改商品信息失败！'
    };
    response.status(500).send(data);
  }
})

// mixApp.post('/list-root_category', rootCategoryMid.gRootCategory, (request, response, next) => {
//   let {root_navs} = request
//
// })

/**
 * 获取商品类目信息
 */
mixApp.post('/list-goods_category', goodsCategoryMid.gGoodsCategoryCount, (request ,response ,next) => { //先获取总记录条数，以计算下面的必要参数

  let { recordCountGoodsCategory } = request

  let size = 20  // 每页显示20条

  request.page = {}  // 创建一个空对象page
  request.page.count = recordCountGoodsCategory.count   //总记录数
  request.page.total = Math.ceil(request.page.count / size) //页数,通过取天花板整数得到
  let {i} = request.body; // 获取当前要展示的页面页数
  console.log('当前要展示的页' + i)
  request.page.index = i ? i : 1   //当前要展示的页面参数,通过url传入,如果有这个参数，就取，没有则为1
  request.page.index = request.page.index > request.page.total ? request.page.total : request.page.index    //当前要展示的页面,如果这个参数大于页面页数,就取页面页数;否则，就取参数
  request.page.index = request.page.index < 1 ? 1 : request.page.index    //当前要展示的页面,如果这个参数小于1，则取1;否则，就取当前参数

  response.start = (request.page.index - 1) * size   //获取从哪个记录条数开始，(当前页-1)*size
  response.size = size   //获取查询几条记录，即每页记录数

  next()

} ,[ goodsCategoryMid.gGoodsCategory, rootCategoryMid.gRootCategory] , (request, response) => {   //这时起始记录数，每页记录数都已经有了，再调用getRecomPage方法即可
  //取到上面的page以及gUserRowCountRecord中间件的指定用户记录信息
  let {recordCountGoodsCategory , page} = request;
  let {root_navs} = request;

  let {id, name, describe, username, root_category_id} = request.body; // 获取前台发来的的条件词，用于将此再次赋给前台以进行特定的渲染

  page.list = recordCountGoodsCategory;
  let res ={
    'code': 200,
    'msg': '获取商品类目成功',
    page,
    id,
    name,
    describe,
    username,
    root_category_id,
    root_navs,
  }
  response.status(200).send(res)
})


/**
 * 获取商品信息
 */
mixApp.post('/list-goods', goodsMid.gGoodsCount, (request, response, next) => {

  let { goodsCount } = request;

  let size = 20;   // 每页显示20条

  request.page = {};  // 创建一个空对象page
  request.page.count = goodsCount.count;   //总记录数
  request.page.total = Math.ceil(request.page.count / size); //页数,通过取天花板整数得到
  let {i} = request.body; // 获取当前要展示的页面页数
  console.log('当前要展示的页' + i);
  request.page.index = i ? i : 1;   //当前要展示的页面参数,通过url传入,如果有这个参数，就取，没有则为1
  request.page.index = request.page.index > request.page.total ? request.page.total : request.page.index;    //当前要展示的页面,如果这个参数大于页面页数,就取页面页数;否则，就取参数
  request.page.index = request.page.index < 1 ? 1 : request.page.index;    //当前要展示的页面,如果这个参数小于1，则取1;否则，就取当前参数

  response.start = (request.page.index - 1) * size;   //获取从哪个记录条数开始，(当前页-1)*size
  response.size = size;   //获取查询几条记录，即每页记录数

  next();
}, [goodsMid.gGoodsRowCountRecord, rootCategoryMid.gRootCategory], (request, response, next) => {

  let {recordCountGoods , page} = request;
  let {root_navs} = request;

  let {id, goods_category_id, root_category_id, username, name_describe_goodsCategoryName_rootCategoryName} = request.body;  // 获取前台发来的的条件词，用于将此再次赋给前台以进行特定的渲染


  page.goods = recordCountGoods;
  let res ={
    'code': 200,
    'msg': '获取商品信息成功',
    page,
    id,
    goods_category_id,
    root_category_id,
    username,
    name_describe_goodsCategoryName_rootCategoryName,
    root_navs,
  };
  response.status(200).send(res);
})

/**
 * 获取订单信息
 */
mixApp.post('/list-order', orderMid.gOrderCount, (request, response, next) => {
  let { orderCount } = request;

  let size = 20;   // 每页显示20条

  request.page = {};  // 创建一个空对象page
  request.page.count = orderCount.count;   //总记录数
  request.page.total = Math.ceil(request.page.count / size); //页数,通过取天花板整数得到
  let {i} = request.body; // 获取当前要展示的页面页数
  console.log('当前要展示的页' + i);
  request.page.index = i ? i : 1;   //当前要展示的页面参数,通过url传入,如果有这个参数，就取，没有则为1
  request.page.index = request.page.index > request.page.total ? request.page.total : request.page.index;    //当前要展示的页面,如果这个参数大于页面页数,就取页面页数;否则，就取参数
  request.page.index = request.page.index < 1 ? 1 : request.page.index;    //当前要展示的页面,如果这个参数小于1，则取1;否则，就取当前参数

  response.start = (request.page.index - 1) * size;   //获取从哪个记录条数开始，(当前页-1)*size
  response.size = size;   //获取查询几条记录，即每页记录数

  next();
}, orderMid.gOrder, (request, response, next) => {
  let {recordCountOrder , page} = request;

  let {username, code, lessAmount, moreAmount, lessDate, moreDate} = request.body
  page.orders = recordCountOrder
  let res = {
    'code': 200,
    'msg': '获取订单信息成功',
    page,
    username,
    'order_code': code,
    lessAmount,
    moreAmount,
    lessDate,
    moreDate,
  }
  response.status(200).send(res);

})

/**
 * 获取订单明细(交易记录)
 */
mixApp.post('/list-order_list', orderListMid.gOrderList, (request, response, next) => {
  let {orderDetail} = request
  let res = {
    'code': 200,
    'msg': '获取订单明细成功',
    orderDetail
  }
  response.status(200).send(res)
})

/**
 * 获取交易详情
 */
mixApp.post('/transaction', orderListMid.gTransactionCount, (request, response, next) => {
  let { orderListCount } = request;

  let {size} = request.body;   // 每页显示20条

  request.page = {};  // 创建一个空对象page
  request.page.count = orderListCount.count;   //总记录数
  request.page.total = Math.ceil(request.page.count / size); //页数,通过取天花板整数得到
  let {i} = request.body; // 获取当前要展示的页面页数
  console.log('当前要展示的页' + i);
  request.page.index = i ? i : 1;   //当前要展示的页面参数,通过url传入,如果有这个参数，就取，没有则为1
  request.page.index = request.page.index > request.page.total ? request.page.total : request.page.index;    //当前要展示的页面,如果这个参数大于页面页数,就取页面页数;否则，就取参数
  request.page.index = request.page.index < 1 ? 1 : request.page.index;    //当前要展示的页面,如果这个参数小于1，则取1;否则，就取当前参数

  response.start = (request.page.index - 1) * size;   //获取从哪个记录条数开始，(当前页-1)*size
  response.size = size;   //获取查询几条记录，即每页记录数

  next();
}, orderListMid.gTransactionList, (request, response, next) => {
  let {recordCountOrderList , page} = request;
  let {id, order_code, buy_username, bus_username, goodsCategoryId, rootCategoryId, lessPrice, morePrice, lessDate, moreDate} = request.body

  page.list = recordCountOrderList
  let res = {
    'code': 200,
    'msg': '获取交易详情成功',
    page,
    id,
    order_code,
    buy_username,
    bus_username,
    goodsCategoryId,
    rootCategoryId,
    lessPrice,
    morePrice,
    lessDate,
    moreDate
  }
  response.status(200).send(res);
})

/**
 * 添加商品类目
 */
mixApp.post('/add-goods_category', goodsCategoryMid.sGoodsCategory, (request, response, next) => {
  if (request.add_gcid) {
    let res = {
      'code': 200,
      'msg': '添加商品类目成功'
    }
    response.status(200).send(res);
  } else {
    let res ={
      'code': 500,
      'msg': '添加商品类目失败'
    }
    response.status(500).send(res)
  }
})

/**
 * 修改商品类目信息
 */
mixApp.post('/update-goods_category', goodsCategoryMid.uGoodsCategory, (request, response, next) => {
  if (request.affectR) {
    let res = {
      'code': 200,
      'msg': '修改商品类目成功'
    }
    response.status(200).send(res)
  } else {
    let res = {
      'code': 500,
      'msg': '修改商品类目失败'
    }
    response.status(500).send(res)
  }
})

/**
 * 删除商品类目
 */
mixApp.post('/delete-goods_category', goodsCategoryMid.dGoodsCategory, (request, response, next) => {
  if (request.affectR) {
    let res = {
      'code': 200,
      'msg': '删除商品类目成功',
    }
    response.status(200).send(res)
  } else {
    let res = {
      'code': 500,
      'msg': '删除商品类目失败'
    }
    response.status(500).send(res)
  }
})

/**
 * 添加消息
 */
mixApp.post('/add-message', messageMid.sMessage, (request, response, next) => {
  if (request.add_mid) {
    let res = {
      'code': 200,
      'msg': '添加消息成功'
    }
    response.status(200).send(res)
  } else {
    let res = {
      'code': 500,
      'msg': '添加消息失败'
    }
    response.status(500).send(res)
  }
})

/**
 * 删除消息
 */
mixApp.post('/delete-message', messageMid.dMessage, (request, response, next) => {
  if (request.affectR) {
    let res = {
      'code': 200,
      'msg': '删除消息成功'
    }
    response.status(200).send(res)
  } else {
    let res = {
      'code': 500,
      'msg': '删除消息失败！！！'
    }
    response.status(500).send(res)
  }
})

// mixApp.post('/list-g-category', [goodsCategoryMid.gGoodsCategoryCount], (request, response, next) => {
//   let { recordCountGoodsCategory } = request;
//
//   let size = 6;   // 每页显示6条
//
//   request.page = {};  // 创建一个空对象page
//   request.page.count = recordCountGoodsCategory.count;   //总记录数
//   request.page.total = Math.ceil(request.page.count / size); //页数,通过取天花板整数得到
//   let {i} = request.body; // 获取当前要展示的页面页数
//   console.log('当前要展示的页' + i);
//   request.page.index = i ? i : 1;   //当前要展示的页面参数,通过url传入,如果有这个参数，就取，没有则为1
//   request.page.index = request.page.index > request.page.total ? request.page.total : request.page.index;    //当前要展示的页面,如果这个参数大于页面页数,就取页面页数;否则，就取参数
//   request.page.index = request.page.index < 1 ? 1 : request.page.index;    //当前要展示的页面,如果这个参数小于1，则取1;否则，就取当前参数
//
//   response.start = (request.page.index - 1) * size;   //获取从哪个记录条数开始，(当前页-1)*size
//   response.size = size;   //获取查询几条记录，即每页记录数
//
//   next();
// }, [goodsCategoryMid.gGoodsCategory], (request, response) => {
//   //取到上面的page以及gUserRowCountRecord中间件的指定用户记录信息
//   let {recordCountGoodsCategory , page} = request;
//   let {id, name, describe, username, root_category_id} = request.body; // 获取前台发来的的条件词，用于将此再次赋给前台以进行特定的渲染
//
//   page.list = recordCountGoodsCategory;
//   let data ={
//     page,
//     id,
//     name,
//     describe,
//     username,
//     root_category_id
//   };
//   response.status(200).send(data);
// });





module.exports = mixApp

